import { Component, OnInit, Input } from '@angular/core';
import { TreeNode, DialogService } from 'primeng/primeng';
import { Router, ActivatedRoute } from '@angular/router';
import { ConfirmationService } from 'primeng/primeng';
import { MdlSnackbarService } from '@angular-mdl/core';
import { QkPartSelectorComponent } from '../../common/component/part/qkpart.component';
import { FreeflowService } from '../freeflow.service';
import { ProcDef, ProcPart } from '../freeflow.model';

@Component({
  selector: 'ff-part',
  templateUrl: './ffpart.component.html',
  styleUrls: ['./ffpart.component.scss'],
  providers: [DialogService]
})
export class FfPartComponent implements OnInit {
  currentProcDefId: number = 0;
  @Input()
  set procDefId(procDefId: number) {
    if (!procDefId) return;
    this.currentProcDefId = procDefId;
    this.freeflowService.listProcParts(procDefId).subscribe(parts => {
      this.parts = [];
      if (parts) {
        parts.forEach(p => {
          if (p.partType == 'H') p.partType = '人员';
          if (p.partType == 'R') p.partType = '岗位';
          if (p.partType == 'U') p.partType = '机构';
          this.parts.push(p);
        });
      }
    })
  }
  get procDefId(): number { return this.currentProcDefId; }

  parts: ProcPart[] = [];
  cols: any[] = [
    { field: 'partId', header: '编号', style: { 'width': '10%', 'text-align': 'center' } },
    { field: 'partType', header: '类型', style: { 'width': '20%', 'text-align': 'center' } },
    { field: 'partName', header: '名称', style: { 'width': '35%', 'text-align': 'center' } },
    { field: 'unitName', header: '所属机构', style: { 'width': '35%', 'text-align': 'center' } }
  ];

  constructor(
    private router: Router,
    private route: ActivatedRoute,
    private dialogService: DialogService,
    private freeflowService: FreeflowService,
    private mdlSnackbarService: MdlSnackbarService,
    private confirmationService: ConfirmationService) {
  }

  ngOnInit() {
  }

  remove(part: ProcPart) {
    this.freeflowService.deleteProcPart(part).subscribe(data => {
      this.mdlSnackbarService.showToast('数据删除成功！');
      this.parts.forEach((ele, i) => {
        if (ele['partId'] == part.partId) {
          this.parts.splice(i, 1);
          return;
        }
      })
    });
  }
  /** 选择参与者 */
  checkParts($event) {
    let checkedNodes = new Array<TreeNode>();
    this.parts.forEach(ele => {
      let node = {
        'id': ele.partId,
        'lable': ele.partName,
        'data': { 'unitCode': ele.unitCode, 'type': ele.partType }
      };
      checkedNodes.push(node);
    });

    const ref = this.dialogService.open(QkPartSelectorComponent, {
      header: '请选择参与者',
      width: '420px',
      contentStyle: { 'height': '550px' },
      data: {
        'ckeckedParts': checkedNodes,
        'authorityCode': 7
      }
    });

    ref.onClose.subscribe((data: any) => {
      let added = [];
      if (data) {
        data.forEach(element => {
          let p = new ProcPart({});
          p.partId = element['id'];
          p.partName = element['label'];
          p.partType = element['data']["type"];
          if (p.partType == 'H') {
            p.humanId = p.partId;
            p.humanName = p.partName;
            p.unitCode = element["parent"]["data"]["code"];
          } else if (p.partType == 'R') {
            p.roleId = p.partId;
            p.roleName = p.partName;
            p.unitCode = element["parent"]["data"]["code"];
          } else if (p.partType == 'U') {
            p.unitCode = element["data"]["code"];
          }
          p.unitId = element["parent"]["id"];
          p.unitName = element["parent"]["label"];
          p.procDefId = this.currentProcDefId;
          let notContain = true;
          this.parts.forEach(sp => {
            if (p.partId == sp.partId) {
              notContain = false;
              return;
            }
          })
          if (notContain) {
            added.push(p);
          }
        });
      }
      if (added.length > 0) {
        this.freeflowService.addProcPart(added).subscribe(data => {
          added.forEach(p => {
            if (p.partType == 'H') p.partType = '人员';
            if (p.partType == 'R') p.partType = '岗位';
            if (p.partType == 'U') p.partType = '机构';
            this.parts.push(p)
          });
          this.mdlSnackbarService.showToast("新增参与者数据保存成功");
        })
      }
    });

  }
}
